﻿using Dm;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System.Data;
using System.Data.Common;
using System.Linq.Expressions;

namespace worker_service.Tools
{
    /// <summary>
    /// 达梦 EF Core 扩展类
    /// </summary>
    public static class DmEfCoreExtension
    {
        /// <summary>
        /// 动态添加查询条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="condition">什么情况下添加查询条件</param>
        /// <param name="expression">要添加的查询条件(lambda 表达式)</param>
        /// <returns></returns>
        public static IQueryable<T> IfWhere<T>
            (this IQueryable<T> query, bool condition, Expression<Func<T, bool>> expression)
        {
            return condition ? query.Where(expression) : query;
        }

        /// <summary>
        /// 执行 sql 返回 DataSet
        /// </summary>
        /// <param name="dbDatabase"></param>
        /// <param name="sql">查询语句</param>
        /// <param name="parms">动态查询参数数组</param>
        /// <returns></returns>
        public static DataSet SqlQueryToDataSet
            (this DatabaseFacade dbDatabase, string sql, params DbParameter[] parms)
        {
            var ds = new DataSet();

            if (string.IsNullOrWhiteSpace(sql)) 
            {
                return ds;
            }

            using var cmd = dbDatabase.GetDbConnection().CreateCommand();
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddRange(parms);

            var da = new DmDataAdapter(cmd as DmCommand);
            da.Fill(ds);

            return ds;
        }

        /// <summary>
        /// 执行 sql 返回 DataTable
        /// </summary>
        /// <param name="dbDatabase"></param>
        /// <param name="sql">查询语句</param>
        /// <param name="parms">动态查询参数数组</param>
        /// <returns></returns>
        public static DataTable SqlQueryToDataTable
            (this DatabaseFacade dbDatabase, string sql, params DbParameter[] parms)
        {
            var dt = new DataTable();

            if (string.IsNullOrWhiteSpace(sql))
            {
                return dt;
            }

            return dbDatabase.SqlQueryToDataSet(sql, parms).Tables[0];
        }

        /// <summary>
        /// 执行 sql 返回单个字符串值
        /// </summary>
        /// <param name="dbDatabase"></param>
        /// <param name="sql">查询语句</param>
        /// <param name="parms">动态查询参数数组</param>
        /// <returns></returns>
        public static string SqlQueryToSingeleString
            (this DatabaseFacade dbDatabase, string sql, params DbParameter[] parms)
        {
            var list = dbDatabase.SqlQueryRaw<string>(sql, parms).ToList();
            if (list.Count > 0)
            {
                return list[0];
            }

            return "";
        }

        /// <summary>
        /// 执行 sql 返回单个 int 值
        /// </summary>
        /// <param name="dbDatabase"></param>
        /// <param name="sql">查询语句</param>
        /// <param name="parms">动态查询参数数组</param>
        /// <returns></returns>
        public static int SqlQueryToSingeleInt
            (this DatabaseFacade dbDatabase, string sql, params DbParameter[] parms)
        {
            var list = dbDatabase.SqlQueryRaw<int>(sql, parms).ToList();
            if (list.Count > 0)
            {
                return list[0];
            }

            return 0;
        }
    }
}
